package com.ricchieri.manage.bean;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.UserFilter;
import com.ricchieri.model.Operation;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.IOperationService;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "addOperationMB")
@ViewScoped
public class AddOperationManageBean extends GenericManageBean {
	private static final long serialVersionUID = 8918890381327705317L;
	
	@ManagedProperty(value = "#{OperationService}")
	IOperationService operationService;
	
	private User user;
	private String operationType;
	private Double amount;
	private String observation;
	
	private UserFilter userFilter;
		
	@PostConstruct
	public void init(){
		user = (User) FacesUtil.getParametersFlash(BusinessSettings.KEY_USER);
		userFilter = (UserFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);

		if(user == null) {
			FacesUtil.redirectToNavigationCase(BusinessSettings.SEARCH_OPERATION);
		}
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public Map<String, String> getOperations(){
		Map<String, String> operations = new LinkedHashMap<String, String>();
		
		operations.put(FacesUtil.getMessage("operationPaid"), BusinessSettings.OPERATION_PAID);
		operations.put(FacesUtil.getMessage("operationReservation"), BusinessSettings.OPERATION_RESERVATION);
		operations.put(FacesUtil.getMessage("operationCancelation"), BusinessSettings.OPERATION_CANCELATION);
		operations.put(FacesUtil.getMessage("operationAdvanced"), BusinessSettings.OPERATION_ADVANCED);
					
		return operations;
	}

	public String getOperationType() {
		return operationType;
	}

	public void setOperationType(String operationType) {
		this.operationType = operationType;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public String getObservation() {
		return observation;
	}

	public void setObservation(String observation) {
		this.observation = observation;
	}
	
	public void reset(){
		operationType = null;
		amount = new Double(0);
		observation = null;
	}
	
	public IOperationService getOperationServiceService() {
		return operationService;
	}

	public void setOperationService(IOperationService operationService) {
		this.operationService = operationService;
	}	
	
	public String save() {
		
		if(this.amount <= 0){
			FacesUtil.addErrorMsg("addOperation:amount", "requiredAmount");
		}else{
			Operation operation = new Operation();
			
			operation.setUser(user);
			operation.setAutomatic(Boolean.FALSE);
			operation.setOperationDate(new Date(System.currentTimeMillis()));
			operation.setOperationType(operationType);
			operation.setAmount(amount);
			operation.setObservation(observation);
			
			operationService.addOperation(operation);
			
			FacesUtil.addParametersFlash(BusinessSettings.KEY_OPEARATION, operation);
			return BusinessSettings.SUCCESS;
		}
		
		return BusinessSettings.UNSUCCESS;
	}
	
	public String doBackToSearchOperation() {
		
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, userFilter);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, user);

		return BusinessSettings.SEARCH_OPERATION;
	}
	
	public String doBackToViewUserAccountStatus() {
		
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, userFilter);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, user);		
		return BusinessSettings.BACK;
	}
}	

